from . import admins_bp
from .forms.welcome import PasswordForm, InfoForm
from ...models import db
from flask import render_template, flash
from flask_login import current_user


@admins_bp.route('/')
@admins_bp.route('/dashboard')
def dashboard():
    return render_template('dashboard.html', page_title='主页')


@admins_bp.route('/change_password/', methods=['GET', 'POST'])
def me_password():
    form = PasswordForm()
    if form.validate_on_submit():
        with db.auto_commit():
            current_user.password = form.password.data
        flash('操作成功', 'success')
    return render_template('me_password.html', form=form, page_title='变更密码')


@admins_bp.route('/info/', methods=['GET', 'POST'])
def me_info():
    form = InfoForm(**current_user.__dict__)
    if form.validate_on_submit():
        with db.auto_commit():
            current_user.from_dict(form.data)
        flash('操作成功', 'success')
    return render_template('me_info.html', form=form, page_title='个人中心')
